package cn.edu.uestc.topo;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONObject;

public class Port {

    private UUID id = null;
    private String name = null;
    private UUID nodeId = null;
    private UUID linkId = null;
    private int type = 1;
    private int status = 1;
    private long bandwidth = 0;
    private String IP = null;
    private long lbl = 0;
    private List<UUID> childPortList = null;

    public Port() {

    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public UUID getNodeId() {
        return nodeId;
    }

    public void setNodeId(UUID nodeId) {
        this.nodeId = nodeId;
    }

    public UUID getLinkId() {
        return linkId;
    }

    public void setLinkId(UUID linkId) {
        this.linkId = linkId;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public List<UUID> getChildPortList() {
        return childPortList;
    }

    public void setChildPortList(List<UUID> childPortList) {
        this.childPortList = childPortList;
    }

    public long getBandwidth() {
        return bandwidth;
    }

    public void setBandwidth(long bandwidth) {
        this.bandwidth = bandwidth;
    }

    public String getIP() {
        return IP;
    }

    public void setIP(String ip) {
        IP = ip;
    }

    public long getLbl() {
        return lbl;
    }

    public void setLbl(long lbl) {
        this.lbl = lbl;
    }

    public void readFromJson(JSONObject jsonObject) {
        try {
            this.id = UUID.fromString(jsonObject.getString("id"));
            this.name = jsonObject.getString("name");
            this.nodeId = UUID.fromString(jsonObject.getString("nodeId"));
            this.linkId = UUID.fromString(jsonObject.getString("linkId"));
            this.type = jsonObject.getInt("type");
            this.status = jsonObject.getInt("status");
            this.bandwidth = jsonObject.getLong("bandwidth");
            this.IP = jsonObject.getString("IP");
            this.lbl = jsonObject.getLong("label");

            JSONArray portJson = jsonObject.getJSONArray("childPortList");

            if (this.childPortList == null)
                this.childPortList = new ArrayList<>();

            for (int i = 0; i < portJson.length(); i++) {
                UUID portID = UUID.fromString(portJson.getString(i));
                this.childPortList.add(portID);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject writeToJson() {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("id", this.id.toString());
            jsonObject.put("name", this.name);
            jsonObject.put("nodeId", this.nodeId.toString());
            jsonObject.put("linkId", this.linkId.toString());
            jsonObject.put("type", this.type);
            jsonObject.put("status", this.status);
            jsonObject.put("bandwidth", this.bandwidth);
            jsonObject.put("IP", this.IP);
            jsonObject.put("label", this.lbl);

            JSONArray portJson = new JSONArray(childPortList);
            jsonObject.put("childPortList", portJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    @Override
    public String toString() {
        String stringContent = null;
        try {
            stringContent = this.writeToJson().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return stringContent;
    }
}
